//
//  ConfigureViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/20.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "ConfigureViewController.h"

#import "UserInfoViewController.h"

#import "ConfigureMessageViewController.h"

#import "ConfigureCell.h"

NSString * const cellIdentifier = @"configureCell";

@interface ConfigureViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation ConfigureViewController

{
    NSArray *_configureList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  fakeValue];
    [self customNav];
    [self setupTableView];
    [self customBottomButton];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

#pragma mark - Custom User InterFace

- (void)fakeValue{
    _configureList = @[@[@"我的账户", @"消息"], @[@"清除缓存", @"检查更新"]];
}

- (void)customNav{
    self.title = @"设置";
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(bbiClick:)];
    self.navigationItem.leftBarButtonItem = bbi;
}

- (void)setupTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, UISCREENHEIGHT) style:UITableViewStyleGrouped];
    [tableView registerClass:[ConfigureCell class] forCellReuseIdentifier:cellIdentifier];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = RGB_COLOR(@"#f6f6f6");
    tableView.scrollEnabled = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
}

- (void)customBottomButton{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bottom - self.tabBarController.tabBar.bounds.size.height, self.view.width, self.tabBarController.tabBar.bounds.size.height)];
    label.backgroundColor = RGB_COLOR(@"#d91f00");
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = @"退出当前账户";
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logOut)];
    [label addGestureRecognizer:tap];
    [self.view addSubview:label];
}

#pragma mark - Actions

- (void)bbiClick:(UIBarButtonItem *)bbi{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)logOut{
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"login"];
    self.navigationController.tabBarController.selectedIndex = 0;
}

#pragma mark - TableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ConfigureCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[ConfigureCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    [cell configureCellModelWithModelName:_configureList[indexPath.section][indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - TableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        UserInfoViewController *uvc = [[UserInfoViewController alloc] init];
        [self.navigationController pushViewController:uvc animated:NO];
    }else if(indexPath.section == 0 && indexPath.row == 1){
        ConfigureMessageViewController *cmvc = [[ConfigureMessageViewController alloc] init];
        [self.navigationController pushViewController:cmvc animated:NO];
    }
}

@end
